<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item
          label="物料"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-tree-select
            style="width:100%"
            :dropdownStyle="{ maxHeight: '200px', overflow: 'auto' }"
            :treeData="treeData"
            v-decorator="[ 'materialId', {rules:[{required: true,message:'不允许为空'}]}]"
            placeholder="请选择物料">
          </a-tree-select>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="发货数量">
          <a-input-number v-decorator="[ 'sendNum', {rules:[{required: true,message:'不允许为空'}]}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="实收数量">
          <a-input-number v-decorator="[ 'receiveNum', {rules:[{required: true,message:'不允许为空'}]}]"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="接收日期">
          <a-date-picker v-decorator="[ 'receiveDate', {rules:[{required: true,message:'不允许为空'}]}]"/>
        </a-form-item>

        <a-form-item label="接收地点" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            style="width: 100%"
            placeholder="请选择接收地点"
            v-decorator="[ 'receiveSite', {rules:[{required: true,message:'不允许为空'}]}]">
            <a-select-option
              v-for="(siteName) in siteNames"
              :key="siteName.text"
              :value="siteName.text">
              {{ siteName.text }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="接收人" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            style="width: 100%"
            placeholder="请选择接收人"
            v-decorator="[ 'receiveUserName', {rules:[{required: true,message:'不允许为空'}]}]">
            <a-select-option
              v-for="(receiveUser) in receiveUsers"
              :key="receiveUser.text"
              :value="receiveUser.text">
              {{ receiveUser.text }}
            </a-select-option>
          </a-select>
        </a-form-item>

      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction, getAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "SsMaterialLeaseRecordModal",
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        selectedMaterial: [],
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        receiveUsers: [],
        siteNames: [],
        treeData: [],
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: "/ss/ssMaterialLeaseRecord/add",
          edit: "/ss/ssMaterialLeaseRecord/edit",
        },
      }
    },
    created() {
      this.initReceiveUser();
      this.intitSiteName();
      this.initMaterialInst();
    },
    methods: {
      add() {
        this.edit({});
      },
      edit(record) {

        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'materialId', 'sendNum', 'receiveNum', 'receiveSite', 'receiveUserName'));
          this.form.setFieldsValue({receiveDate: this.model.receiveDate ? moment(this.model.receiveDate) : moment(new Date())});
          // this.selectedMaterial = new Array().push(this.model.materialId);
        });

      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      initReceiveUser() {
        getAction("/sys/dict/getDictItems/receiveUser", "").then((res) => {
          if (res.success) {
            this.receiveUsers = res.result;
          }
        });
      },
      initMaterialInst() {
        getAction("/ss/ssMaterialInst/queryMaterialTree", "").then((res) => {
          if (res.success) {
            this.treeData = res.result;
          }
        });
      },
      intitSiteName() {
        getAction("/sys/dict/getDictItems/siteName", "").then((res) => {
          if (res.success) {
            this.siteNames = res.result;
          }
        });
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.receiveDate = formData.receiveDate ? formData.receiveDate.format() : null;

            console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }
        })
      },
      handleCancel() {
        this.close()
      },


    }
  }
</script>

<style scoped>

</style>